﻿namespace Redmist.UsageMonitor.WebIO
{
    using System.IO;
    using System.Net;

    // this isnt currently working - a deserialize exception is thrown when attempting to map the xml back to a DataUsageModel type
    // the json retrieve method works however, so will go with that

    /// <summary>Retrieve usage data from the aaNet web site via the Usage web service</summary>
    public static class WebServiceRetriever
    {        


        /// <summary>Retrieves usage statistics from the aaNet web site.</summary>
        /// <param name="serviceNumber">A System.String representing an aaNet user service number.</param>
        /// <param name="password">A System.String representing a password for the aaNet member's account.</param>
        /// <returns>A Redmist.UsageMonitor.WebIO.UsageRetrievalResult representing a usage data retrieval result.</returns>
        public static UsageRetrievalResult RetrieveUsage(string serviceNumber, string password, string url = "")
        {            
            UsageRetrievalResult usageRetrievalResult = new UsageRetrievalResult();

            try
            {
                var usageService = new Redmist.UsageMonitor.WebServiceProxy.Usage();
                if (url != null)
                {
                    usageService.Url = url;
                }
                var aanetUsageModel = usageService.GetUsageForPhoneAndPassword(phoneNumber: serviceNumber, password: password);
                usageRetrievalResult.MonthlyUsageResult = new Redmist.UsageMonitor.UsageData.MonthlyUsage(aanetUsageModel);
                usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.OK;
            }
            catch (WebException webException)
            {
                switch (webException.Status)
                {
                    case WebExceptionStatus.Timeout:
                        usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.Timeout;
                        break;
                    default:
                        usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.ConnectionError;
                        break;
                }
            }
            catch (System.Web.Services.Protocols.SoapException se)
            {
                System.Diagnostics.Trace.WriteLine("SoapException: " + se.Message);
                usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.ProtocolError;
            }
            catch (System.Exception e)
            {
                System.Diagnostics.Trace.WriteLine("Exception: " + e.Message);
                usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.ProtocolError;
            }

            return usageRetrievalResult; 
        }

        public static UsageRetrievalResult RetrieveUsage(string hashCode, string url = "")
        {            
            UsageRetrievalResult usageRetrievalResult = new UsageRetrievalResult();

            try
            {
                var usageService = new Redmist.UsageMonitor.WebServiceProxy.Usage();
                if (url != null)
                {
                    usageService.Url = url;
                }
                var aanetUsageModel = usageService.GetUsageForHashCode(hashCode: hashCode);
                usageRetrievalResult.MonthlyUsageResult = new Redmist.UsageMonitor.UsageData.MonthlyUsage(aanetUsageModel);
                usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.OK;
            }
            catch (WebException webException)
            {
                switch (webException.Status)
                {
                    case WebExceptionStatus.Timeout:
                        usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.Timeout;
                        break;
                    default:
                        usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.ConnectionError;
                        break;
                }
            }
            catch (System.Web.Services.Protocols.SoapException se)
            {
                System.Diagnostics.Trace.WriteLine("SoapException: " + se.Message);
                usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.ProtocolError;
            }
            catch (System.Exception e)
            {
                System.Diagnostics.Trace.WriteLine("Exception: " + e.Message);
                usageRetrievalResult.UsageRetrievalStatus = UsageRetrievalStatus.ProtocolError;
            }

            return usageRetrievalResult;             
        }    
    }
}
